package com.starpl.mobile.android.monthunsae.november.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.monthunsae.november.domain.UserInfo;
import com.starpl.mobile.android.monthunsae.november.handler.CommunityListHandler;
import com.starpl.mobile.android.platform.common.base.BaseActivity;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.component.StarplListView;


/**
 * 추가된 커뮤니티(추가된 커뮤니티 공통)
 * 
 * @author 우연화
 * @since 2011.03.09
 */
public class CommunityActivity extends BaseActivity implements OnClickListener
{
	private final int LOVE_UNSAE = 10084402;
	private final int MONEY_UNSAE = 10001374;

	private ImageButton mIbRefresh;
	private ImageButton mIbWrite;

	private ImageButton mIbCalendar;
	private LinearLayout mLlCalendar;

	private LinearLayout mLlCommunityLayout;

	private CommunityListHandler mListHandler;
	private int mKeywordIdx;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.starpl.platform.common.base.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.community_activity);

		mIbRefresh = (ImageButton) findViewById(R.id.community_activity_ib_refresh);
		mIbWrite = (ImageButton) findViewById(R.id.community_activity_ib_write);
		mIbCalendar = (ImageButton) findViewById(R.id.community_activity_ib_calendar);
		mLlCalendar = (LinearLayout) findViewById(R.id.community_activity_ll_calendar);
		mLlCommunityLayout = (LinearLayout) findViewById(R.id.community_activity_ll_community);

		mIbRefresh.setOnClickListener(this);
		mIbWrite.setOnClickListener(this);
		mIbCalendar.setOnClickListener(this);

		UserInfo userInfo = (UserInfo) BaseApplication.getGlobalPersistent("userInfo");
		mKeywordIdx = getIntent().getExtras().getInt("keywordIdx", 0);

		// 연애 커뮤니티가 아니고 재태크 커뮤니티가 아닌 경우
		if(mKeywordIdx == LOVE_UNSAE || mKeywordIdx == MONEY_UNSAE)
		{
			mLlCalendar.setVisibility(LinearLayout.VISIBLE);
		}
		else
		{
			mLlCalendar.setVisibility(LinearLayout.GONE);
		}

		mListHandler = new CommunityListHandler(this, userInfo.getUserId(), mKeywordIdx);
		StarplListView listView = new StarplListView(mListHandler);
		mLlCommunityLayout.addView(listView);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.starpl.platform.common.base.BaseActivity#onResume()
	 */
	@Override
	protected void onResume()
	{
		super.onResume();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v)
	{
		Bundle extras;

		switch(v.getId())
		{
		case R.id.community_activity_ib_refresh:
			mListHandler.refresh();
			break;
		case R.id.community_activity_ib_write:
			extras = new Bundle();
			extras.putInt("keywordIdx", getIntent().getIntExtra("keywordIdx", 0));
			extras.putInt("type", WriteActivity.TYPE_POST);

			getNewActivity().setRequestCode(1).setTargetActivity(WriteActivity.class).setBundle(extras).show();
			break;
		case R.id.community_activity_ib_calendar:
			extras = new Bundle();
			extras.putInt("keywordIdx", getIntent().getIntExtra("keywordIdx", 0));
			getNewActivity().setTargetActivity(UnsaeCalendarActivity.class).setBundle(extras).show();
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode == RESULT_OK)
		{
			mListHandler.refresh();
		}
	}
}
